/**
* 自定义方法类，用于存储第三方方法
**/

//时间转换方法
function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

//图片等比缩放
function autoImage(e,windowWidth,windowHeight){
    var windowscale = windowHeight/windowWidth;//屏幕高宽比
    //获取图片的原始长宽
    var originalWidth = e.detail.width;
    var originalHeight = e.detail.height;
    var originalScale = originalHeight/originalWidth;//图片高宽比 
    var results= {};
    if(originalScale < windowscale){//图片高宽比小于屏幕高宽比 
      //图片缩放后的宽为屏幕宽 
      results.imageWidth = windowWidth; 
      results.imageHeight = (windowWidth * originalHeight) / originalWidth; 
    }else{//图片高宽比大于屏幕高宽比 
      //图片缩放后的高为屏幕高 
      results.imageHeight = windowHeight; 
      results.imageWidth = (windowHeight * originalWidth) / originalHeight; 
    } 
    return results;
}

//HTML编码
function htmlEncode(str)  
{  
  var s = "";  
  if (str.length == 0) return "";  
  s = str.replace(/&/g, "&gt;");  
  s = s.replace(/</g, "&lt;");  
  s = s.replace(/>/g, "&gt;");  
  s = s.replace(/ /g, "&nbsp;");  
  s = s.replace(/\'/g, "&#39;");  
  s = s.replace(/\"/g, "&quot;");  
  s = s.replace(/\n/g, "<br>"); 
  return s;  
}  

//HTML解码
function htmlDecode(str)  
{  
  var s = "";  
  if (str.length == 0) return "";  
  s = str.replace(/&gt;/g, "&");  
  s = s.replace(/&lt;/g, "<");  
  s = s.replace(/&gt;/g, ">");  
  s = s.replace(/&nbsp;/g, " ");  
  s = s.replace(/&#39;/g, "\'");  
  s = s.replace(/&quot;/g, "\"");  
  s = s.replace(/<br>/g, "\n");  
  s = s.replace(/<p>/g,"\n");
  s = s.replace(/<\/p>/g,"");
  s = s.replace(/<p&/g,"");
  s = s.replace(/<br&/g,"");
  s = s.replace(/<font&/g,"");
  s = s.replace(/<\/p&/g,"\n");
  s = s.replace(/<\/font&/g,"");
  s = s.replace(/&amp;/g,"");
   s = s.replace(/nbsp;/g,"");
  return s;  
}

//格式化服务器时间
function removeHyphen(str) {//只是取年与日
  return str.replace(/-/g, "").substring(0, 8);
}

function cutOutStr(str) {//统一处理截取-电话为:用户名
  return str.replace(/-/g,"").substring(0,3)+"***"+str.replace(/-/g,"").substring(7,11)
 }
//计算行星显示规则
function starCount(originStars) {
  //计算星星显示需要的数据，用数组stars存储五个值，分别对应每个位置的星星是全星、半星还是空星
  var starNum = originStars * 10 / 10, stars = [], i = 0;
  do {
    if (starNum >= 1) {
      stars[i] = 'full';
    } else if (starNum >= 0.5) {
      stars[i] = 'half';
    } else {
      stars[i] = 'no';
    }
    starNum--;
    i++;
  } while (i < 5)
  return stars;
}
//  添加星星
function updateStart(arr) {
  let stars = []
  for (let i = 0; i < arr.length; i++) {
    let item = arr[i];
    item.stars = starCount(item.commentScore)
  }
}
// 验证中文名称 
function isChinaName(name) {
  var pattern = /^[\u4E00-\u9FA5]{1,6}$/;
  return pattern.test(name);
}
// 验证手机号 
function isPhoneNo(phone) {
  var pattern = /^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$/;
  return phone.test(pattern);
}

// 验证身份证  
function isCardNo(card) {
  var pattern = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  console.log(pattern.test(card))
  return pattern.test(card);
} 
// 获取当日时间
function getNowFormatDate() {
  var date = new Date();
  var seperator1 = "-";
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = year + seperator1 + month + seperator1 + strDate;
  return currentdate;
}
module.exports = {
  formatTime: formatTime,
  autoImage: autoImage,
  htmlEncode: htmlEncode,
  htmlDecode: htmlDecode,
  removeHyphen: removeHyphen,
  cutOutStr: cutOutStr,
  starCount: starCount,
  updateStart: updateStart,
  isChinaName: isChinaName,
  isPhoneNo: isPhoneNo,
  isCardNo: isCardNo,
  getNowFormatDate: getNowFormatDate
}
